<?php

class work_View extends View {
     
    public function createView($action, $params) {
        switch($action) {
            case 'startWorkError':
            case 'stopWorkError':
            case 'closeTaskError':
                if($action == 'startWorkError') {
                    $this->view .= $this->errorInfo("Nie udało się rozpocząć pracy nad zadaniem.");
                } elseif($action == 'stopWorkError') {
                    $this->view .= $this->errorInfo("Nie udalo się zatrzymać pracy nad zadaniem.");
                } else {
                    $this->view .= $this->errorInfo("Nie udalo się zamknąć zadania.");
                }
                unset($params['action']);
                unset($params['wid']);
                unset($params['ptid']);
            case 'menu': 
                $user = TheUser::getInstance();
                $work = new Work();
                if($work->getCurrentWork($user)) {
                    $task = new ProjectTask((int)$work->getPtid());
                    $project = new Project((int)$task->getPid());
                    $this->view .= 'Aktualnie pracujesz nad zadaniem: "' . $project->name . '" -> "' . $task->name . '" (' . $work->getTimeStart() . ').';
                    $seconds = time() - strtotime($work->getTimeStart());
                    $this->view .= '<br />Aktualny czas wykonywania: <span class="time" id="' . $seconds. '"></span><br />';
                    $buttons = array();
                    $buttons[] = array('action'=>'endWork', 'text'=>'Zakończ pracę nad zadaniem','onclick'=>'return confirm(\'Czy na pewno chcesz zakończyć pracę nad tym zadaniem?\');');
                    $buttons[] = array('action'=>'endTask', 'text'=>'Zamknij zadanie','onclick'=>'return confirm(\'Czy na pewno chcesz zamknąć to zadanie?\');');
                    $params['ptid'] = $work->getPtid();
                    $params['wid'] = $work->getWid();
                    $this->view .= Menu::createMenu($buttons, $params, true);
                } else {
                    $tasks = ProjectDAO::getSignedTasks($user, true);
                    $options = array();
                    $options[] = array('id'=>0,'text'=>'--- Wybierz zadanie ---');
                    foreach($tasks as $key => $task) {
                        $project = new Project((int)$task->getPid());
                        $options[] = array('id'=>$task->getPtid(),'text'=>$project->name . ' -> ' . $task->name);
                    }
                    $f = new Form(View::createLink($params), 'post');
                        $f->addSection("Wybór zadania", "chooseTask");
                            $f->section()->add(new Select("", $options, "ptid", 0));
                        $f->addButton(new Input("", "submit", '', 'Rozpocznij pracę'));
                        $f->addButton(new Input("", "button", '', 'Anuluj', '','','onClick="location.href = location.href.split(\'?\')[0];"'));
                    $this->view .= $f->get();
                }
                $worksDone = $work->getWorkDone(strtotime(date("Y-m-d")), strtotime(date("Y-m-d") . " +1 day")-1, false, TheUser::getInstance());
                $table = '<table class="workDone">
                            <thead>
                                <tr><th colspan="5">Wykonane prace</th></tr>
                                <tr>
                                    <th>Projekt</th>
                                    <th>Zadanie</th>
                                    <th>Start</th>
                                    <th>Koniec</th>
                                    <th>Czas</th>
                                </tr>
                            </head>
                            <tbody>';
                foreach($worksDone as $key => $workStd) {
                    $table .= '<tr>';
                    $project = new Project((int)$workStd->pid);
                    $task = new ProjectTask((int)$workStd->ptid);
                    $table .= '<td>' . $project->name . '</td>';
                    $table .= '<td>' . $task->name . '</td>';
                    $table .= '<td>' . $workStd->time_start . '</td>';
                    $table .= '<td>' . $workStd->time_end . '</td>';
                    $table .= '<td>' . $work->getWorkTimeString($workStd) .  '</td>';
                    $table .= '</tr>';
                }
                $table .= '</tbody></table>';
                $this->view .= '<br />' . $table;
            break;
        
        }
    }
    
}

?>
